package test;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.Path2D;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JComponent;

import obj.Edge;
import obj.Node;
import obj.Path;

public class CarComponent extends JComponent {
	public CarComponent() {
		setBackground(Color.GREEN);
		setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
	}

	private static final long serialVersionUID = -5421059417481228683L;
	private ArrayList<CarTest> cars = new ArrayList<CarTest>();

	public synchronized void add(CarTest car) {
		cars.add(car);
	}

	public void paintComponent(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;
		/*
		 * Draw Paths
		 */
		for (CarTest car : cars) {
			g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

			g2.setColor(Color.lightGray);
			g2.setStroke(new BasicStroke(20.0f, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND));

			Shape s = getShape(car.getPath());
			g2.draw(s);

		}
		/*
		 * Draw cars
		 */
		for (CarTest car : cars) {
			Point2D.Double p = car.getPosition();

			g2.setStroke(new BasicStroke(2.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND));
			g2.setPaint(Color.BLUE);

			// Rectangle2D.Double box = new Rectangle2D.Double(p.getX(),
			// p.getY(), 12, 8);

//			Ellipse2D.Double box = new Ellipse2D.Double(p.getX(), p.getY(), 10, 10);
//
//			AffineTransform tx = new AffineTransform();
//			tx.rotate(car.getCarAngle());
//			Shape newShape = tx.createTransformedShape(box);
//
//			g2.draw(newShape);
//			g2.fill(newShape);
			g2.draw(car.getShape());
			g2.fill(car.getShape());
//			g2.setPaint(Color.BLACK);
//			g2.drawString(car.name, (int) p.getX(), (int) p.getY());

			g2.setPaint(Color.BLACK);
			g2.fillOval(car.getPath().getEdgesList().get(0).getStartNode().getxPoint(), car.getPath().getEdgesList()
					.get(0).getStartNode().getyPoint(), 5, 5);
			g2.fillOval(car.getPath().getEdgesList().get(0).getEndNode().getxPoint(),
					car.getPath().getEdgesList().get(0).getEndNode().getyPoint(), 5, 5);
		}

	}

	private Path2D getShape(Path p) {
		Path2D pathShape;
		ArrayList<Path2D> shapesList = new ArrayList<Path2D>();
		// for (Path p : pathsList) {
		pathShape = new Path2D.Float();
		List<Edge> edges = p.getEdgesList();
		int count = 0;
		int[] xpoints = new int[edges.size() * 2];
		int[] ypoints = new int[edges.size() * 2];
		int xValue;
		int yValue;

		for (Edge e : edges) {
			Node sNode = e.getStartNode();
			xValue = sNode.getxBox() * 10;
			yValue = sNode.getyBox() * 10;
			xpoints[count] = xValue;
			ypoints[count] = yValue;
			count++;
			Node eNode = e.getEndNode();
			xValue = eNode.getxBox() * 10;
			yValue = eNode.getyBox() * 10;
			xpoints[count] = xValue;
			ypoints[count] = yValue;
			count++;
		}

		for (int i = 0; i < xpoints.length; i++) {
			if (i == 0) {
				pathShape.moveTo(xpoints[i], ypoints[i]);
			} else {
				pathShape.lineTo(xpoints[i], ypoints[i]);
			}
		}

		shapesList.add(pathShape);

		// }

		return pathShape;
	}
}
